////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.menu.display {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormatAlign;
	
	import plantool.core.CoreDisplayObject;
	import plantool.helpers.TextFieldHelper;
	import plantool.modules.menu.data.MenuGroupData;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class MenuGroup extends CoreDisplayObject {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _actionGroup:MenuGroupData	= null;
		private var _tf:TextField				= null;
		private var _width:Number				= 200;
		private var _height:Number				= 75;
		private var _items:Vector.<MenuItem>	= new Vector.<MenuItem>();
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public override function get height():Number {
			return _height;
		}
		public override function set height(value:Number):void {
			//set width
			_height = value;
			
			//invalidate
			invalidate();
		}
		
		public override function get width():Number {
			return _width;
		}
		public override function set width(value:Number):void {
			//set width
			_width = value;
			
			//invalidate
			invalidate();
		}
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function MenuGroup(grp:MenuGroupData) {
			//store group
			_actionGroup = grp;
			
			super();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		protected override function init():void {
			//create textfield
			_tf			= TextFieldHelper.createTitleTf(TextFieldAutoSize.NONE, TextFormatAlign.CENTER, false, false, 0);
			addChild(_tf);
			
			//create items
			for (var i:uint = 0; i < _actionGroup.items.length; i++) {
				var itm:MenuItem 	= new MenuItem(_actionGroup.items[i]);
				itm.y				= (_tf.y + _tf.height) + (_items.length * itm.height);
				itm.width			= _width;
				addChild(itm);
				
				_height				= itm.y + itm.height + 5;
				
				_items.push(itm);
			}
			
			//call super init
			super.init();
		}
		
		public override function draw():void {
			//set tf
			_tf.text 	= _actionGroup.title;
			_tf.width	= _width;
		}
	}
}